﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using RedPanda.MusicPlayer.DataModel;
using System.IO;

using WMPLib;

namespace RedPanda.MusicPlayer.Utils
{
    public class MusicPlayerUtils
    {

        private static WindowsMediaPlayer wmp;


        public static WindowsMediaPlayer GetWmpInstance()
        {
            if (wmp == null)
            {
                wmp = new WindowsMediaPlayer();
                wmp.settings.autoStart = false;
            }
            return wmp;
        }
        /// <summary>
        /// 根据专辑名和歌手名称生成唯一的专辑ID
        /// </summary>
        /// <param name="title"></param>
        /// <param name="artist"></param>
        /// <returns></returns>
        public static string GetAlbumId(string title, string artist)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes=System.Text.Encoding.UTF8.GetBytes((title + "." + artist).ToUpper());
            byte[] result = md5.ComputeHash(bytes);

            bytes = md5.ComputeHash(bytes); 
            md5.Clear(); 

            string ret = ""; 
            for (int i = 0; i < bytes.Length; i++) 
            { 
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0'); 
            } 

            return ret.PadLeft(32, '0'); 
          }

        /// <summary>
        /// 从指定的文件夹中读取音频文件，生成MediaLibrary类;
        /// </summary>
        /// <param name="dirs"></param>
        /// <returns></returns>
        public static MediaLibrary GenerateMediaLibrary(DirectoryInfo[] dirs)
        {
            //支持的文件类型;
            List<string> supportedFileType = new List<string>();
            supportedFileType.Add(".mp3");

           MediaLibrary mediaLibrary=new MediaLibrary();

            foreach (DirectoryInfo dir in dirs)
            {
                //所有子文件;
                FileInfo[] files = dir.GetFiles();
                //所有子文件夹
                DirectoryInfo[] subDirs = dir.GetDirectories();

                if(files.Length>0)
                {
                    foreach(FileInfo f in files)
                    {
                        if(supportedFileType.Contains(f.Extension))
                        {
                            //根据该文件获得音频文件的信息，并且添加至MeidaLibrary；
                            AudioFile af = new AudioFile(f.FullName);
                            mediaLibrary.Tracks.Add(af);

                            //获得专辑封面的字节数据;
                            byte[] data=CoverArt.GetCoverBytes(af.Location);
                            //生成该曲目的专辑类
                            Album album = new Album(af.Album, af.Artist,af.Genre, af.Year, 0,data);
                            int index = mediaLibrary.Albums.IndexOf(album);
                            if (index<0)
                                mediaLibrary.Albums.Add(album);
                            else
                            {
                                Album target=mediaLibrary.Albums[index];
                                target.TrackNum += 1;
                                //如果专辑还没有封面则尝试从该文件中读取封面;
                                if (target.CoverLocation.Equals(string.Empty) &&
                                    !album.CoverLocation.Equals(string.Empty))
                                    target.CoverLocation = album.CoverLocation;
                            }

                            Artist artist = new Artist(af.Artist);
                             if(!(mediaLibrary.Artists.Contains(artist)))
                                 mediaLibrary.Artists.Add(artist);
                        }
                    }
                }

                //遍历子文件夹
                if(subDirs.Length>0)
                {
                    MediaLibrary ml=MusicPlayerUtils.GenerateMediaLibrary(subDirs);
                    //将找到的AudioFile加入到MediaLibrary中;
                    mediaLibrary.Tracks.AddRange(ml.Tracks);

                    //判断找到的Album和Artist是否已经包括在内，是则跳过，否则加入
                    foreach(Album album in ml.Albums)
                    {
                        int index=-1;
                        if ((index=mediaLibrary.Albums.IndexOf(album))>=0)
                        {
                            
                           mediaLibrary.Albums[index].TrackNum+=1;
                        }
                        else
                        {
                            mediaLibrary.Albums.Add(album);
                        }
                    }

                    foreach (Artist artist in ml.Artists)
                    {
                        if (!mediaLibrary.Artists.Contains(artist))
                        {
                            mediaLibrary.Artists.Add(artist);
                        }
                    }
                    
                }
            }
            return mediaLibrary;
        }

        /// <summary>
        /// 根据歌手的名称获得歌手ID
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetArtistId(string name)
        {
            return name.ToUpper().GetHashCode() + "";
        }

        /// <summary>
        /// 获得给定的音乐文件的时长;
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static double GetAudioDuration(string path)
        {
            WindowsMediaPlayer player = MusicPlayerUtils.GetWmpInstance();
            try
            {
                player.URL = path;
            
                return wmp.currentMedia.duration;
            }
            catch (Exception e)
            {
                
                return 0;
            }
        }
    }
}
